package com.xyoto.community.controller;

import com.xyoto.community.aspect.annotation.RequiresLogin;
import com.xyoto.community.dto.*;
import com.xyoto.community.service.DynamicService;
import com.xyoto.community.service.PictureService;
import com.xyoto.community.utils.PageUtils;
import com.xyoto.community.utils.Result;
import com.xyoto.community.vo.DynamicCommentsVo;
import com.xyoto.community.vo.DynamicDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:   动态前台管理Web接口
 * Author:      挽风
 * Date:        2020
 * Copyright:   2020 by 挽风1.0版本
 * Description:
 **/
@RestController
@RequestMapping("/dynamic")
@Api(value = "DynamicController", tags = "动态前台接口（前台）")
public class DynamicController {

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private PictureService pictureService;

    /**
     * 新增动态评论回复
     *   @param saveDynamicCommentReplyDTO
     *   @return
     */
    @RequiresLogin
    @ApiOperation("新增动态评论回复")
    @PostMapping("/saveDynamicCommentReply")
    public Result  saveDynamicCommentReply(@RequestBody @Valid SaveDynamicCommentReplyDTO saveDynamicCommentReplyDTO){
        pictureService.checkContent(new CheckContentDTO(saveDynamicCommentReplyDTO.getContent()));
        return dynamicService.saveDynamicCommentReply(saveDynamicCommentReplyDTO);
    }

    /**
     * 新增动态Web接口
     *
     * @param saveDynamicDTO 动态表单DTO
     *
     * @return 操作状态
     */
    @RequiresLogin
    @ApiOperation(value = "新增动态Web接口", notes = "新增动态Web接口", httpMethod = "POST")
    @PostMapping("/saveDynamic")
    public Result saveDynamic(@Validated @RequestBody SaveDynamicDTO saveDynamicDTO){
        pictureService.checkContent(new CheckContentDTO(saveDynamicDTO.getContent()));
        return dynamicService.saveDynamic(saveDynamicDTO);
    }

    /**
     * 新增动态评论Web接口
     *
     * @param saveDynamicCommentDTO 动态评论表单DTO
     *
     * @return 操作状态
     */
    @RequiresLogin
    @ApiOperation(value = "新增动态评论Web接口", notes = "新增动态评论Web接口", httpMethod = "POST")
    @PostMapping("/saveDynamicComment")
    public Result saveDynamicComment(@Valid @RequestBody SaveDynamicCommentDTO saveDynamicCommentDTO){
        pictureService.checkContent(new CheckContentDTO(saveDynamicCommentDTO.getContent()));
        return dynamicService.saveDynamicComment(saveDynamicCommentDTO);
    }

    /**
     * 修改动态Web接口
     *
     * @param updateDynamicDTO
     *
     *
     * 修改后的动态表单DTO
     *
     * @return 修改状态
     */
    @RequiresLogin
    @ApiOperation(value = "修改动态Web接口", notes = "修改动态Web接口", httpMethod = "POST")
    @PostMapping("/updateDynamic")
    public Result updateDynamic(@RequestBody UpdateDynamicDTO updateDynamicDTO) {
        pictureService.checkContent(new CheckContentDTO(updateDynamicDTO.getContent()));
        return dynamicService.updateDynamic(updateDynamicDTO);
    }

    /**
     * 新增动态点赞
     *
     * @param saveDynamicLoveDTO
     * @return
     */
    @RequiresLogin
    @ApiOperation("新增动态点赞")
    @PostMapping("/saveDynamicLove")
    public Result saveDynamicLove(@RequestBody @Valid SaveDynamicLoveDTO saveDynamicLoveDTO){
        return dynamicService.saveDynamicLove(saveDynamicLoveDTO);
    }

    /**
     * 删除动态
     *
     * @param deleteDynamicDTO 动态id
     * @return
     */
    @RequiresLogin
    @ApiOperation("删除动态")
    @DeleteMapping("/removeDynamic")
    public Result removeDynamicById(@RequestBody DeleteDynamicDTO deleteDynamicDTO) {
        return dynamicService.removeDynamicById(deleteDynamicDTO.getDynamicId());
    }

    /**
     * 删除动态评论
     *
     * @param deleteDynamicCommentDTO 动态评论id
     * @return
     */
    @RequiresLogin
    @ApiOperation("删除动态评论")
    @DeleteMapping("removeDynamicComment")
    public Result removeDynamicComment(@RequestBody DeleteDynamicCommentDTO deleteDynamicCommentDTO) {
        return dynamicService.removeDynamicCommentByDynamicCommentId(deleteDynamicCommentDTO.getDynamicCommentId());
    }

    /**
     * 删除动态评论回复
     *
     * @param deleteDynamicCommentReplyDTO 回复id
     * @return
     */
    @RequiresLogin
    @ApiOperation("删除动态评论回复")
    @DeleteMapping("removeDynamicCommentReply")
    public Result removeDynamicCommentReply(@RequestBody DeleteDynamicCommentReplyDTO deleteDynamicCommentReplyDTO) {
        return dynamicService.removeDynamicCommentReply(deleteDynamicCommentReplyDTO.getDynamicCommentReplyId());
    }

    /**
     * 新增动态评论点赞
     *
     * @param saveDynamicCommentLoveDTO
     * @return
     */
    @RequiresLogin
    @ApiOperation("新增动态评论点赞")
    @PostMapping("/saveDynamicCommentLove")
    public Result saveDynamicCommentLove(@RequestBody @Valid SaveDynamicCommentLoveDTO saveDynamicCommentLoveDTO) {
        return dynamicService.saveDynamicCommentLove(saveDynamicCommentLoveDTO);
    }

    /**
     * 取消动态点赞
     * @param deleteDynamicLoveDTO 动态id
     */
    @RequiresLogin
    @ApiOperation("取消动态点赞")
    @DeleteMapping("/removeDynamicLove")
    public Result removeDynamicLoveByDynamicId(@RequestBody DeleteDynamicLoveDTO deleteDynamicLoveDTO){
        return dynamicService.removeDynamicLoveByDynamicId(deleteDynamicLoveDTO.getDynamicId());
    }

    /**
     * 取消动态评论点赞
     * @param deleteDynamicCommentLoveDTO 动态评论id
     */
    @RequiresLogin
    @ApiOperation("取消动态评论点赞")
    @DeleteMapping("/removeDynamicCommentLove")
    public Result removeDynamicCommentLoveByDynamicCommentId(@RequestBody DeleteDynamicCommentLoveDTO deleteDynamicCommentLoveDTO){
        return dynamicService.removeDynamicCommentLoveByDynamicCommentId(deleteDynamicCommentLoveDTO.getDynamicCommentId());
    }

    @RequiresLogin
    @ApiOperation("新增动态评论回复点赞")
    @PostMapping("/saveDynamicCommentReplyLove")
    public Result saveDynamicCommentReplyLove(@RequestBody @Valid SaveDynamicCommentReplyLoveDTO saveDynamicCommentReplyLoveDTO) {
        return dynamicService.saveDynamicCommentReplyLove(saveDynamicCommentReplyLoveDTO);
    }

    @ApiOperation("获取全部动态列表")
    @GetMapping("/getTotalDynamicList")
    public Result getTotalDynamicList(@Valid PageDTO pageDTO) {
        return dynamicService.getTotalDynamicList(pageDTO);
    }

    @ApiOperation("获取热门动态列表")
    @GetMapping("/getHotDynamicList")
    public Result getHotDynamicList(@Valid PageDTO pageDTO) {
        return dynamicService.getHotDynamicList(pageDTO);
    }

    /**
     * 获取动态详情
     * @param dynamicId 动态id
     * @return
     */
    @ApiOperation("动态详情")
    @GetMapping("/getDynamicByDynamicId")
    public Result<DynamicDetailVo> getDynamicByDynamicId(@RequestParam(value = "dynamicId") Integer dynamicId) {
        return dynamicService.getDynamicByDynamicId(dynamicId);
    }

    /**
     * 评论回复列表详情
     *
     * @return
     */
    @ApiOperation("评论回复列表详情")
    @GetMapping("/listCommentReplyByCommentId")
    public Result listCommentReplyByCommentId(ListCommentReplyDTO listCommentReplyDTO){
         return dynamicService.listCommentReplyByCommentId(listCommentReplyDTO);
    }

    /**
     * 取消动态评论回复点赞
     * @param deleteDynamicCommentReplyLoveDTO 动态评论回复id
     * @return
     */
    @RequiresLogin
    @ApiOperation("取消动态评论回复点赞")
    @DeleteMapping("/removeDynamicCommentReplyLove")
    public Result removeDynamicCommentReplyLove(@RequestBody DeleteDynamicCommentReplyLoveDTO deleteDynamicCommentReplyLoveDTO){
        return dynamicService.removeDynamicCommentReplyLove(deleteDynamicCommentReplyLoveDTO.getDynamicCommentReplyId());
    }

    @ApiOperation("获取动态评论列表")
    @GetMapping("/getDynamicCommentList")
    public Result<List<DynamicCommentsVo>> getDynamicCommentList(@Valid GetDynamicCommentDTO getDynamicCommentDTO ) {
        return dynamicService.getDynamicCommentList(getDynamicCommentDTO.getId(),getDynamicCommentDTO);
    }

    @RequiresLogin
    @ApiOperation("动态设为私密")
    @PostMapping("/markDynamicAsPersonal")
    public Result markDynamicAsPersonal( @Valid @RequestBody MarkDynamicPrivateDTO markDynamicPrivateDTO){
        return dynamicService.markDynamicAsPersonal(markDynamicPrivateDTO.getDynamicId());
    }

    @ApiOperation("根据条件查询动态列表")
    @GetMapping("/getDynamicListByCondition")
    public Result getDynamicListByCondition(DynamicSearchConditionDTO dynamicSearchConditionDTO){
        if (dynamicSearchConditionDTO.getTopicId() == null && StringUtils.isBlank(dynamicSearchConditionDTO.getContent())){
            return Result.success(PageUtils.restPage(new ArrayList<>()));
        }
        return dynamicService.getDynamicListByCondition(dynamicSearchConditionDTO,dynamicSearchConditionDTO);
    }

}
